﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils.DatabaseUtils;
using Utils;

namespace Entity
{
    public class AdminUI
    {
        public Admin Login(String username, String password)
        {
            try
            {
                Admin result = AdminInfo(SqlHelper.ExecuteDataset(Common.Conn, "Admin_Login", username, password));
                return result;
            }
            catch (Exception e) { return null; }
        }

        private Admin AdminInfo(DataSet ds)
        {
            try
            {
                if (TableLib.HasData(ds))
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    Admin ae = new Admin();
                    ae.Username = row["Username"].ToString().Trim();
                    ae.FullName = row["FullName"].ToString();
                    ae.Mobile = row["Mobile"].ToString();
                    ae.Address = row["Address"].ToString();
                    ae.Email = row["Email"].ToString();
                    ae.BirhDay = Convert.ToDateTime(row["BirhDay"].ToString());  
                    ae.Level = Convert.ToByte(row["Level"].ToString());
                    return ae;
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int Add(string Username, string Password, string FullName, string Mobile, string Address, string Email, DateTime? BirhDay, byte Level)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Admin_Add", Username,Password, FullName, Mobile, Address, Email, BirhDay,Level);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int Update(string Username, string FullName, string Mobile, string Address, string Email, DateTime? BirhDay, byte Level)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Admin_Update", Username, FullName, Mobile, Address, Email, BirhDay,Level);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int Delete(string Username)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Admin_Delete", Username);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int DeleteAll()
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Admin_DeleteAll");
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public DataSet Get(string Username)
        {
            try
            {
                DataSet result = SqlHelper.ExecuteDataset(Common.Conn, "Admin_Get", Username);
                return result;
            }
            catch (Exception e) { return null; }
        }

        public DataSet GetAll()
        {
            try
            {
                DataSet result = SqlHelper.ExecuteDataset(Common.Conn, "Admin_GetAll");
                return result;
            }
            catch (Exception e) { return null; }
        }

        public int ChangePass(string Username, string oldPass, string newPass)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Admin_ChangePass", Username,oldPass,newPass);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int Edit(string Username, string Pass)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Admin_Edit", Username, Pass);
                return result;
            }
            catch (Exception e) { return -1; }
        }

    }




    public class Admin
    {

        string _Username;
        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }
  
        string _Password;
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        
        string _FullName;
        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value; }
        }
        
        string _Mobile;
        public string Mobile
        {
            get { return _Mobile; }
            set { _Mobile = value; }
        }
        
        string _Address;
        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        
        string _Email;
        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
        
        DateTime _BirhDay;
        public DateTime BirhDay
        {
            get { return _BirhDay; }
            set { _BirhDay = value; }
        }

        byte _Level;
        public byte Level
        {
            get { return _Level; }
            set { _Level = value; }
        }
    
    }
}
